package com.simplemvc.core;

import com.simplemvc.core.util.ActionMapping;
import com.simplemvc.core.vo.ActionItem;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 王海
 */
public class ActionServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String requestPath = req.getRequestURI();// 用户访问的资源地址
            requestPath = requestPath.substring(req.getContextPath().length() + 1).split("\\.do")[0];
            System.err.println("requestPath:" + requestPath);
            ActionItem item = ActionMapping.getActionItem(requestPath);
            try {
                Class instanceClass = Class.forName(item.getClassName());
                Method[] methods = instanceClass.getDeclaredMethods();
                for (Method method : methods) {
                    if (method.getName().equals(item.getMethod())) {
                        Class paramType = method.getParameterTypes()[0];
                        Object param = paramType.newInstance();
                        Field[] fields = paramType.getDeclaredFields();
                        for (Field field : fields) {
                            field.setAccessible(true);
                            field.set(param, req.getParameter(field.getName()));
                            field.setAccessible(false);
                        }
                        ActionForm form =(ActionForm) param;
                        form.setRequest(req);
                        Object instance = instanceClass.newInstance();
                      
                        String result = (String) method.invoke(instance, param);
                        String resultUrl = ActionMapping.getResultUrl(requestPath, result);
                        RequestDispatcher dis = req.getRequestDispatcher(resultUrl);
                        dis.forward(req, resp);
                        return;
                    }
                }
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();;
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            } catch (SecurityException ex) {
                Logger.getLogger(ActionServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            RequestDispatcher dis = req.getRequestDispatcher("");
            dis.forward(req, resp);
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
    }

}
